<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput> 
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<!-- <view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view> -->
			</view>
			
			<!-- 底部信息 -->
			<view class="footer"> 
				<navigator  @click='forgetClick()' open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url='register' open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from 'components/watch-login/watch-input.vue' //input
	import wButton from 'components/watch-login/watch-button.vue' //button
	import c from '../modules/common.js'
	import b from '../modules/bll.js'
	
	
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			//this.isLogin();
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
			forgetClick(){
				c.show("请联系管理员找回密码");
			},
			// goRegisterPage()
			// {
			// 	c.openPage('register');
			// },
		    startLogin(e){
                console.log(e)
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 3) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码太弱'
		            });
		            return;
		        }
					_this.isRotate=true
					b.login(this.phoneData,this.passData,(data)=>{
						if(data.Status==1)
						{
							c.show('登录成功');
							c.delay(1,()=>{
								c.closePage();
							});
							// c.closePage();
						}
						_this.isRotate=false
					})
				
				
		    },
			login_weixin() {
				console.log('====》微信登录');
				
				
				// uni.login({
				// 	provider: 'weixin',
				// 	success: (loginRes) =>{
						
				// 		console.log('loginRes='+JSON.stringify(loginRes));
						
				// 		 b.getHttpWithParam('action=wxaccesstoken&code='+loginRes.code,(data)=>{
				// 		 	console.log('wxaccesstoken===>'+JSON.stringify(data));
				// 		 });
						 
				// 		}
				// 		});
				// 	return;
						
				// 获取用户信息
				if(c.isPlatformWeiXin())
				{
					c.loading();
					uni.login({
						provider: 'weixin',
						success: (loginRes) =>{
						
							
							console.log('loginRes='+JSON.stringify(loginRes))
							
							c.loading();
							b.getHttpWithParam("action=wxmpgetopenidbycode&code="+loginRes.code,(data)=>{
								console.log("wxmpgetopenidbycode==>"+JSON.stringify(data));
								c.hideLoading();
								if(data.Status==1)
								{
									
								loginRes.openid=data.Data.openid;				
								 // let loginedDict={
									//  openid:data.Data.openid
								 // }
								 console.log('getUserProfile start=====>'+JSON.stringify(loginRes));
								 c.loading();
								// uni.getUserProfile({
									uni.getUserInfo({
										provider: 'weixin',
										desc:"请求授权登录获取头像等信息",
										success: (infoRes)=> {
											console.log('infoRes='+JSON.stringify(infoRes));
											
											//解密encryptedData，可获取用户openId
											// let pc = new WXBizDataCrypt(appid, this.session_key);
											// let data = pc.decryptData(res.encryptedData, res.iv);
											// console.log('解密后：',data)
																	
											this.loginServer(loginRes, infoRes, 'wx');
										},
										fail:(error)=>{
											console.log('getUserProfile== failed'+JSON.stringify(error));
										},
										complete:() =>{
											c.hideLoading()
										}
									});
									}
									
							});
							
						
				
						},
						complete:()=>{
								c.hideLoading();
						}
						});
				}
				else
				{
					//微信登录
					uni.login({
						provider: 'weixin',
						success: (loginRes) =>{
							
							console.log('loginRes='+JSON.stringify(loginRes))
							
						
								 
								
							uni.getUserInfo({
								provider: 'weixin',
								success: (infoRes)=> {
									console.log('infoRes='+JSON.stringify(infoRes));
									
									this.loginServer(loginRes, infoRes, 'wx');
								}
							});
							
						}
					});
				}
				// uni.showToast({
				// 	icon: 'none',
				// 	position: 'bottom',
				// 	title: '...'
				// });
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			//授权登录
			loginServer(loginRes, infoRes, type) {
				
				
				let _this = this;
				let url;
				let pram = {};
				// _this.loginRes=JSON.stringify(loginRes).toString();
				// _this.infoRes=JSON.stringify(infoRes).toString();
				switch (type) {
					case 'qq':
						url = '/token/sys/login-qq';
						pram = {
							'openid': loginRes.authResult.openid,
							'nickname': infoRes.userInfo.nickname,
							'sex': infoRes.userInfo.gender,
							'province': infoRes.userInfo.province,
							'city': infoRes.userInfo.city,
							'headimgurl': infoRes.userInfo.figureurl_qq
						}
						break;
					case 'wx':
						// let userInfo=JSON.stringify(loginRes.rawData);
						let userInfo=infoRes["userInfo"];
						console.log('userInfo==='+JSON.stringify(userInfo))
						url = '/token/sys/login-wechat';
						pram = {
							'openid': loginRes.openid,
							'nickname': userInfo.nickName,
							'sex': userInfo.gender,
							'province': userInfo.province,
							'city': userInfo.city,
							'country': userInfo.country,
							'headimgurl': userInfo.avatarUrl,
							'unionid': loginRes.unionid
						}
						break;
					default:
				}
				
				_this.isRotate=true;
				b.loginWx(pram,(data)=>{
					
					if(data.Status==1)
					{
						c.show('登录成功');
						c.delay(1,()=>{
							c.closePage();
						});
					}
					_this.isRotate=false
				});
				
					
				
				// _this.isRotate=true
				// b.login(this.phoneData,this.passData,(data)=>{
				// 	if(data.Status==1)
				// 	{
				// 		c.show('登录成功');
				// 		c.delay(1,()=>{
				// 			c.closePage();
				// 		});
				// 		// c.closePage();
				// 	}
				// 	_this.isRotate=false
				// })
				
			}		
		}
	}
</script>

<style>
	@import url("/components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
